﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using EZCRS.HCMS.Web;
using EZCRS.HCMS.Bll;
using EZCRS.HCMS.Entity;
using EZCRS.HCMS.Bll.AIR;


namespace EZCRS.HCMS.Web.manage.AIR
{
    public partial class notify_list : ManageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (base.BasHotelId == 0)
                Response.Write(CFun.AlertMeg(Resources.Site.MustHotel, "back", ""));

            //CheckModule(70);

            if (Request.QueryString["action"] != null)
            {
                //get data
                if (Request.QueryString["action"].ToLower() == "get")
                {
                    int iPageIndex = 1;
                    int iPageSize = 15;
                    int iState = 1;

                    if (!string.IsNullOrEmpty(Request.Params["page"]))
                        int.TryParse(Request.Params["page"], out iPageIndex);

                    if (!string.IsNullOrEmpty(Request.Params["rp"]))
                        int.TryParse(Request.Params["rp"], out iPageSize);


                    Response.Write(GetJSON(Request.Params["query"], iPageIndex, iState, iPageSize));
                    Response.End();
                }
                else if (Request.QueryString["action"].ToLower() == "del")
                {
                    int iID = 0;

                    if (!string.IsNullOrEmpty(Request.Params["id"]))
                        int.TryParse(Request.Params["id"], out iID);

                    int iback = DoDelete(iID);

                    if (iback == 0)
                        Response.Write("{state:1}");
                    else
                        Response.Write("{state:0}");

                    Response.End();
                }
                else if (Request.QueryString["action"].ToLower() == "send")
                {
                    string strChannels = string.Empty;
                    string strIATA = string.Empty;
                    string strNotifyIDs = string.Empty;
                    string strProtocol = string.Empty;

                    if (Request.QueryString["chl"] != null)
                        strChannels = Request.QueryString["chl"];

                    if (Request.QueryString["iata"] != null)
                        strIATA = Request.QueryString["iata"];

                    if (Request.QueryString["p"] != null)
                        strProtocol = Request.QueryString["p"];

                    if (Request.Params["id"] != null)
                        strNotifyIDs = Request.Params["id"];

                    int iRows = DoSend(strChannels, strIATA, strNotifyIDs, strProtocol);


                    Response.Write("{ rows:" + iRows.ToString() + "}");
                    Response.End();
                }

            }
        }

        /// <summary>
        /// 发送通知
        /// </summary>
        /// <param name="ChannelIDs"></param>
        /// <param name="IATAs"></param>
        /// <param name="NotifyIDs"></param>
        /// <param name="Protocol"></param>
        private int DoSend(string ChannelIDs, string IATAs, string NotifyIDs, string Protocol)
        {
            int iSent = 0;

            AIR_Notify.NotifyProtocol protocol = AIR_Notify.NotifyProtocol.Fax;

            if (Protocol.ToLower() == "email")
                protocol = AIR_Notify.NotifyProtocol.Email;

            if (!string.IsNullOrEmpty(NotifyIDs))
            {
                string[] strNotifyIDs = NotifyIDs.Split(',');

                foreach (string strNotifyID in strNotifyIDs)
                {
                    int iNotifyID = 0;

                    if (int.TryParse(strNotifyID, out iNotifyID))
                    {
                        AIR_Notify notifyBll = new AIR_Notify();

                        if (!string.IsNullOrEmpty(ChannelIDs))
                        {
                            ChannelIDs = ChannelIDs.TrimEnd(',');

                            string[] strChannels = ChannelIDs.Split(',');

                            foreach (string strChannel in strChannels)
                            {
                                int i = 0;
                                int.TryParse(strChannel, out i);
                                
                                int iRows = notifyBll.SendNotify(iNotifyID, protocol, i);

                                iSent += iRows;
                            }
                        }

                        if (!string.IsNullOrEmpty(IATAs))
                        {
                            IATAs = IATAs.TrimEnd(',');

                            string[] strIATAs = IATAs.Split(',');

                            foreach (string strIATA in strIATAs)
                            {
                                notifyBll.SendNotify(iNotifyID, protocol, strIATA);

                                iSent++;
                            }
                        }
                    }
                }

            }

            return iSent;

        }

        private int DoDelete(int ID)
        {
            EZCRS.HCMS.Bll.AIR.AIR_Notify notify = new EZCRS.HCMS.Bll.AIR.AIR_Notify();

            return notify.DeleteNotify(ID);
        }

        private string GetJSON(string Query, int PageIndex, int State, int PageSize)
        {

            EZCRS.HCMS.Bll.AIR.AIR_Notify notify = new EZCRS.HCMS.Bll.AIR.AIR_Notify();

            int iPageCount = 0;
            int iRecordCount = 0;

            List<HCRS_AIR_NOTIFY> lst = notify.GetItemsByPID(base.BasPropertyId, Query, PageIndex, PageSize, out iRecordCount, out iPageCount);

            string strJson = "{\"total\":" + iRecordCount + ",\"page\":" + PageIndex.ToString() + ",\"rows\":[";

            foreach (HCRS_AIR_NOTIFY plan in lst)
            {
                strJson += "{\"id\":" + plan.ID.ToString() + ",";
                strJson += "\"cell\":[";

                strJson += "\"" + plan.ID.ToString() + "\","; //id

                strJson += "\"" + HttpUtility.HtmlEncode(plan.Name) + "\",";
                strJson += "\"" + plan.CreateDate.ToString() + "\"";

                strJson += "]},";
            }

            strJson = strJson.Replace("\r\n", " ");
            strJson = strJson.TrimEnd(',');
            strJson += "]}";

            return strJson;
        }
    }
}
